package com.qianchang.microfinance.view.mine;

import android.os.Bundle;
import android.view.View;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.FormatUtil;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.element.DTextView;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.BankCardBean;
import com.qianchang.microfinance.bean.FindUserBasicInfoBean;
import com.qianchang.microfinance.bean.MineUserInfo;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.view.dialog.alert.DimengSingleButtonAlertDialog;
import com.qianchang.microfinance.view.home.HomeFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述: 我的钱包
 * 修 改 人: zhoulai
 * 修改时间: 2017/8/14 16:56
 * //                1	loginName	String	用户名
 * //            2	createTime	String	时间
 * //            3	type	String	收入支出类型：0.收入 1.支出
 * //            4	amountIn	String	收入
 * //            5	amountOut	String	支出
 * //            balance	String	本笔支付之后的帐户余额
 * //            6	description	String	备注
 * //            7	tradeName	String	支付名称
 */
public class MyWalletActivity extends BaseActivity {

    @BindView(R.id.tv_my_money)
    DTextView tvMyMoney;
    @BindView(R.id.tv_right)
    DTextView tvRight;
    private FindUserBasicInfoBean findUserBasicInfoBean = new FindUserBasicInfoBean();
    private BankCardBean bankCardBean;
    private MineUserInfo mineUserInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_wallet);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitleName(getString(R.string.title_my_wallet));
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText(getString(R.string.Label_trad_record));
    }

    @Override
    protected void initData() {
        super.initData();
        Bundle bundle = getBundle();
        if (bundle != null) {
            mineUserInfo = (MineUserInfo) bundle.getSerializable(MineUserInfo.class.getName());
            findUserBasicInfoBean = (FindUserBasicInfoBean) bundle.getSerializable(ConstantAppInfo.BASIC_INFO_BEAN);
            if (findUserBasicInfoBean == null) findUserBasicInfoBean = new FindUserBasicInfoBean();
            //改成用接口请求
            if (mineUserInfo != null) {
                tvMyMoney.setText(FormatUtil.numKbPointFormat(mineUserInfo.getAvailable()) + "");
            }
        }
        getBankCardInfo();//获取银行卡信息
    }

    @Override
    protected void onResume() {
        super.onResume();
        getUserInfo();
    }

    public void getUserInfo() {
        show();
        HttpParams params = new HttpParams();
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.FIND_USER_INFO, params, new RequestCallBack<MineUserInfo>() {
            @Override
            public void onSuccess(JsonResult<MineUserInfo> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    if (null != response.getObject()) {
                        mineUserInfo = response.getObject();
                        tvMyMoney.setText(FormatUtil.numKbPointFormat(mineUserInfo.getAvailable()) + "");
                    } else {
                        ToastUtil.getInstant().show(MyWalletActivity.this, getString(R.string.data_error));
                    }
                } else {
                    ToastUtil.getInstant().show(MyWalletActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(MyWalletActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });
    }

    @OnClick({R.id.btn_recharge, R.id.btn_withdraw, R.id.tv_right})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.tv_right) {
            startActivity(TradRecordActivity.class);       //支付记录
            return;
        }
        String tips = "";
        ////是否添加银行卡绑定，0：是、1：否
        if (0 == findUserBasicInfoBean.getIsBankCardBind()) {
            Bundle bundle = new Bundle();
            bundle.putSerializable(BankCardBean.class.getName(), bankCardBean);
            bundle.putSerializable(HomeFragment.ENTITY_KEY, findUserBasicInfoBean);
            bundle.putSerializable(MineUserInfo.class.getName(), mineUserInfo);

            switch (view.getId()) {
                case R.id.btn_recharge:
                    startActivity(RechargeActivity.class, bundle);//充值
                    break;
                case R.id.btn_withdraw:
                    startActivity(WithDrawActivity.class, bundle);//提现
                    break;
            }
        } else {
            switch (view.getId()) {
                case R.id.btn_recharge:
                    tips = getString(R.string.please_bind_card_before_recharge);
                    break;
                case R.id.btn_withdraw:
                    tips = getString(R.string.please_bind_card_before_withdraw);
                    break;
            }
            DimengSingleButtonAlertDialog singleButtonAlertDialog = DimengSingleButtonAlertDialog.newInstance("", tips);
            singleButtonAlertDialog.setAgreeText(getString(R.string.btn_sure));
            singleButtonAlertDialog.setOnDialogClickListener(new DimengSingleButtonAlertDialog.OnDialogClickListener() {
                @Override
                public void agree() {
                    MyWalletActivity.this.finish();
                }
            });
            singleButtonAlertDialog.show(getSupportFragmentManager(), getClass().getSimpleName());
        }
    }

    //获取绑定的银行卡信息
    public void getBankCardInfo() {
        show();
        HttpParams params = new HttpParams();
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.FIND_CARD_DETAIL, params, new RequestCallBack<BankCardBean>() {

            @Override
            public void onSuccess(JsonResult<BankCardBean> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    bankCardBean = response.getObject();
                } else {
                    ToastUtil.getInstant().show(MyWalletActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(MyWalletActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });

    }
}
